package com.controller;

import com.bean.*;
import com.service.MenuUrlService;
import com.service.PaperService;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Ming on 2016/10/11.
 */

@Controller
@RequestMapping("submenu")
public class SubMenuController {

    @Autowired
    UserService userService;
    @Autowired
    MenuUrlService menuUrlService;
    @Autowired
    PaperService paperService;

    @RequestMapping("/article-list.do")
    public String getArticleList(HttpServletRequest request) {
        request.setAttribute("article-list","JSP/article/article-list");
        return null;
    }

    @RequestMapping("/member-list.do")
    public String getMemberList(HttpServletRequest request) {
        List<User> userslist = userService.getUserAll();
        request.setAttribute("userslist",userslist);
        List<Student> studentlist = userService.getStudentAll();
        request.setAttribute("studentlist",studentlist);
        List<Teacher> teacherlist = userService.getTeacherAll();
        request.setAttribute("teacherlist",teacherlist);
        return "JSP/member/member-list";
    }

    @RequestMapping("/member-add.do")
    public String getMemberAdd(HttpServletRequest request) {
//        request.setAttribute("article-list","JSP/article/article-list");
        return "JSP/member/member-add";
    }
    @RequestMapping(value = "/member-edit")
    public String updateUserById(HttpServletRequest request)throws NumberFormatException{
        String userName = request.getParameter("userName");
        List<User> user = userService.getUserByUserName(userName);
        List<Student> student = userService.getStudentByUserName(userName);
        List<Teacher> teacher = userService.getTeacherByUserName(userName);
        if (student!=null){
            request.setAttribute("getUserName",student.get(0).getUserName());
            request.setAttribute("getStatusName",student.get(0).getStatusName());
            request.setAttribute("getuserByUserNameToUpdate",student);
        }if (teacher!=null){
            request.setAttribute("getUserName",teacher.get(0).getUserName());
            request.setAttribute("getStatusName",teacher.get(0).getStatusName());
            request.setAttribute("getuserByUserNameToUpdate",teacher);
        }if (user!=null){
            request.setAttribute("getUserName",user.get(0).getUserName());
            request.setAttribute("getStatusName",user.get(0).getStatusName());
            request.setAttribute("getuserByUserNameToUpdate",user);
        }

        return "JSP/member/member-edit";
    }

    @RequestMapping("/menu-list.do")
    //查询所有菜单信息
    public String getAllMenuMsg(HttpServletRequest request){
        List<MenuUrl> menuMsgList = menuUrlService.getAllMenuMsg();
        request.setAttribute("menuMsgList",menuMsgList);

        return "JSP/menu/menu-list";
    }

    @RequestMapping("/menu-add")
    public String menuAdd(){
        return "JSP/menu/menu-add";
    }
    @RequestMapping("/menu-update")
    public String menuUpdate(HttpServletRequest request){
        Long id = Long.parseLong(request.getParameter("id"));
        request.setAttribute("menuId",id);
        List<MenuUrl> menuUrlByIdList = menuUrlService.getMenuMsgById(id);
        request.setAttribute("menuUrlByIdList",menuUrlByIdList);
        return "JSP/menu/menu-update";
    }

    @RequestMapping("/paper-add")
    public String paperAdd(HttpServletRequest request){
        return "JSP/paper/paper-add";
    }

    @RequestMapping("/paper-list")
    public String paperList(HttpServletRequest request){

        List<Student> notChoiceTeacherStudent=userService.getNotChoiceTeacherStudent();
        request.setAttribute("notChoiceTeacherStudent",notChoiceTeacherStudent);

        List<PaperCustom> notChoicePaperStudent = paperService.getNotChoicePaperStudent();
        request.setAttribute("notChoicePaperStudent",notChoicePaperStudent);

        List<PaperCustom> allpapermsg = paperService.getAllPaperMsg();
        request.setAttribute("allpapermsg",allpapermsg);
        return "JSP/paper/paper-list";
    }

    @RequestMapping("/paper-edit")
    public String paperEdit(HttpServletRequest request){
        List<Teacher> teacher = userService.getAllTeacher();
        Long id = Long.parseLong(request.getParameter("paperchoiceid"));
        List<PaperCustom> papermsg=paperService.getPaperMsgById(id);
        request.setAttribute("paperchoiceid",id);
        request.setAttribute("papermsg",papermsg);
        request.setAttribute("allTeacher",teacher);
        return "JSP/paper/paper-edit";
    }
    @RequestMapping("/paper-edit-choiceteacher")
    public String paperEditChoiceTeacher(HttpServletRequest request){
        Long studentid = Long.parseLong(request.getParameter("studentid"));
        request.setAttribute("studentid",studentid);
        List<Student> studentlist = userService.getStudentById(studentid);
        request.setAttribute("studentlist",studentlist);
        List<Teacher> allteacher = userService.getAllTeacher();
        request.setAttribute("allteacher",allteacher);
        return "JSP/paper/paper-edit-choiceteacher";
    }
    @RequestMapping("/paper-edit-replaceteacher")
    public String paperEditReplaceTeacher(HttpServletRequest request){
        Long choiceid = Long.parseLong(request.getParameter("choiceid"));
        request.setAttribute("choiceid",choiceid);
        List<Student> studentlist = paperService.getStudentByChoiceId(choiceid);
        request.setAttribute("studentlist",studentlist);
        List<Teacher> allteacher = userService.getAllTeacher();
        request.setAttribute("allteacher",allteacher);
        return "JSP/paper/paper-edit-replaceteacher";
    }

    @RequestMapping("/paper-teacher")
    public String paperTeacher(HttpServletRequest request){
        Long userid = Long.parseLong(request.getParameter("userid"));
        List<Paper> papermsg= paperService.getPaperByTeacher(userid);
        List<PaperCustom> choicepaperstudentmsg=paperService.getChoicePaperStudentByTeacher(userid);
        List<PaperCustom> notchoicepaperstudentmsg=paperService.getNotChoicePaperStudentByTeacher(userid);
        request.setAttribute("papermsg",papermsg);
        request.setAttribute("choicepaperstudentmsg",choicepaperstudentmsg);
        request.setAttribute("notchoicepaperstudentmsg",notchoicepaperstudentmsg);
        request.setAttribute("userid",userid);
        return "JSP/paper/paper-teacher-list";
    }
    @RequestMapping("/paper-teacher-add")
    public String paperTeacherAdd(HttpServletRequest request){
        Long userid = Long.parseLong(request.getParameter("userid"));
        request.setAttribute("userid",userid);
        return "JSP/paper/paper-teacher-add";
    }
    @RequestMapping("/paper-teacher-editpapertitle")
    public String paperTeacherEditPaperTitle(HttpServletRequest request){
        Long paperid = Long.parseLong(request.getParameter("paperid"));
        List<Paper> getpaperById=paperService.getPaperById(paperid);
        request.setAttribute("getpaperById",getpaperById);
        request.setAttribute("paperid",paperid);
        return "JSP/paper/paper-teacher-editpapertitle";
    }
    @RequestMapping("/paperchoice-edit")
    public String paperChoiceEdit(HttpServletRequest request){

        return "JSP/paper/paperchoice-edit";
    }

    @RequestMapping("/paper-student-list")
    public String paperStudentList(HttpServletRequest request){
        Long userid = Long.parseLong(request.getParameter("userid"));
        request.setAttribute("userid",userid);
        List<PaperCustom> studentpaperchoice = paperService.getStudentPaperChoice(userid);
        request.setAttribute("studentpaperchoice",studentpaperchoice);
        if (paperService.getPaperByStudentId(userid).get(0).getPaper()!=null){
            request.setAttribute("paper","已提交");
        }
        List<PaperCustom> studentnotpaperchoice = paperService.getStudentNotPaperChoice(userid);
        request.setAttribute("studentnotpaperchoice",studentnotpaperchoice);
        List<Paper> studentpaperList = paperService.getOptionalPaperTitle(userid);
        request.setAttribute("studentpaperList",studentpaperList);

/*
            List<Paper> studentpaperList = paperService.getAllPaperTitle(userid);
            request.setAttribute("studentpaperList",studentpaperList);
*/
        return "JSP/paper/paper-student-list";
    }
    @RequestMapping("/paper-student-addtitle")
    public String paperStudentAddTitle(HttpServletRequest request){
        request.setAttribute("userid",request.getParameter("userid"));
        return "JSP/paper/paper-student-addtitle";
    }
    @RequestMapping("/paper-teacher-edit")
    public String paperTeacherEdit(HttpServletRequest request){
        Long paperchoiceid = Long.parseLong(request.getParameter("paperchoiceid"));
        request.setAttribute("paperchoiceid",paperchoiceid);
        return "JSP/paper/paper-teacher-edit";
    }
    @RequestMapping("/paper-student-addpaper")
    public String paperStudentAddPaper(HttpServletRequest request){
        Long userid = Long.parseLong(request.getParameter("userid"));
        request.setAttribute("userid",userid);
        return "JSP/paper/paper-student-addpaper";
    }

}
